package com.sanyue.contest;

import org.junit.Test;

import java.util.*;

/**
 * @author sanyue
 * @version 1.0
 * @description: TODO-竞赛
 * @date 2022/1/23 上午10:31
 */
public class One {

    @Test
    public void test(){
        List<Integer> ints = findLonely(new int[]{75,35,59,66,69,53,37,16,60,98,11,33,3,85,59,65,59,44,34,89,72,47});
        for (int i = 0; i < ints.size(); i++) {
            System.out.println(ints.get(i));
        }
    }


    public List<Integer> findLonely(int[] nums) {
        ArrayList<Integer> ans = new ArrayList<Integer>();
        HashSet<Integer> set = new HashSet<Integer>();
        HashSet<Integer> set2 = new HashSet<Integer>();
        HashSet<Integer> set3 = new HashSet<Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (set.contains(nums[i])){
                set2.add(nums[i]);
                continue;
            }
            set.add(nums[i]);
            set3.add(nums[i]);
        }
        ArrayList<Integer> list2 = new ArrayList<Integer>(set2);
        for (int i = 0; i < list2.size(); i++) {
            if (set.contains(list2.get(i))) set.remove(list2.get(i));
        }
        //set中放了所有无重复的数据 去原来的进行查询
        ArrayList<Integer> list = new ArrayList<Integer>(set);
        for (int i = 0; i < list.size(); i++) {
            if (!set3.contains(list.get(i)-1)&&!set3.contains(list.get(i)+1))
            ans.add(list.get(i));
        }
        return ans;
    }


    public int[] rearrangeArray(int[] nums) {
        LinkedList<Integer> queue1= new LinkedList<Integer>();
        LinkedList<Integer> queue2= new LinkedList<Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (nums[i]>0)queue1.add(nums[i]);
            if (nums[i]<0)queue2.add(nums[i]);
        }
        int[] ans = new int[nums.length];
        int index = 0;
        while (queue1.size()>0&&queue2.size()>0){
            ans[index]=queue1.poll();
            index++;
            ans[index]=queue2.poll();
            index++;
        }
        return ans;
    }

    public int countElements(int[] nums) {
        if (nums.length < 3) return 0;
        int max = nums[0];
        int min = nums[0];
        int maxcount = 0;
        int mincount = 0;

        for (int i = 0; i < nums.length; i++) {
            if (nums[i]>max){
                max = nums[i];
                maxcount = 0;
            }
            if (nums[i] == max){
                maxcount +=1;
            }
            if (nums[i]<min){
                min = nums[i];
                mincount = 0;
            }
            if (nums[i] == min){
                mincount +=1;
            }
        }
        if (max == min)return 0;
        return nums.length-maxcount-mincount;
    }
}
